package com.shop.shop.controller;



import com.shop.shop.entity.Collect;
import com.shop.shop.service.CollectService;
import com.shop.shop.utile.JWTUtils;
import com.shop.shop.utile.Q;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author 小🌤
 * @since 2021-04-22
 */
@RestController
@RequestMapping("/shop/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    /***
     * 查询收藏
     * @param cmId
     * @param request
     * @return
     */
    @GetMapping("info")
    public Q INFO(Integer cmId, HttpServletRequest request) {
        Map<String, Object> map1 = JWTUtils.verify(request.getHeader("token"));
        Object id = map1.get("id");
        Collect collect = collectService.findByCmIdAndurId(cmId, id);
        boolean B = collect != null ? true : false;

        return Q.ok().data("b", B);
    }

    /**
     * 保存收藏
     *
     * @param collect
     * @param request
     * @return
     */
    @PostMapping("save")
    public Q save(@RequestBody Collect collect, HttpServletRequest request) {
        Map<String, Object> map1 = JWTUtils.verify(request.getHeader("token"));
        String id = (String) map1.get("id");
        collect.setUrId(Integer.parseInt(id));
        boolean save = collectService.save(collect);
        return save ? Q.ok() : Q.error();
    }

    /**
     * 取消收藏
     *
     * @param cmId
     * @param request
     * @return
     */
    @DeleteMapping("delete")
    public Q delete(Integer cmId, HttpServletRequest request) {
        Map<String, Object> map1 = JWTUtils.verify(request.getHeader("token"));
        Object id = map1.get("id");
        int i = collectService.deletecmIdAndurId(cmId, id);
        return i > 0 ? Q.ok() : Q.error();
    }

}

